<%@page import="javax.print.attribute.standard.Finishings"%><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*,dao.*,pojo.*,java.lang.reflect.*"%>
<% 		
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");	
	 
    String methodName = request.getParameter("method");
    
    try{
	    Class[] argsClass = new Class[2]; 
	    argsClass[0] = HttpServletRequest.class;
	    argsClass[1] = HttpServletResponse.class;
	    
	    Class cls = this.getClass();   
	    Method method = cls.getMethod(methodName, argsClass);   
	    
	    Object[] args = new Object[2];
	    args[0] = request;
	    args[1] = response;   
	    
	    BeforeInvoke(methodName);
	    method.invoke(this, args);     
    }catch( Exception e){
        HashMap result = new HashMap();
        result.put("error", -1);
        result.put("message", e.getMessage());
        result.put("stackTrace", e.getStackTrace());
        String json = Test.JSON.Encode(result);
        response.reset();
        response.getWriter().write(json);
    }        
    finally
    {
        AfterInvoke(methodName);
    }
%>
<%!//权限管理
protected void BeforeInvoke(String methodName)
{
    //Hashtable user = GetUser();
    //if (user.role == "admin" && methodName == "remove") throw .      
}
//日志管理
protected void AfterInvoke(String methodName)
{
	
}
//////////////////////////////////////
public void SearchEmployees(HttpServletRequest request, HttpServletResponse response) throws Exception
{ 		
    //查询条件
    String key = request.getParameter("key");
    //分页
    int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));        
    //字段排序
    String sortField = request.getParameter("sortField");
    String sortOrder = request.getParameter("sortOrder");
	System.out.println("key="+key+"\npageIndex="+pageIndex+"\npageSize="+pageSize+"\nsortField="+sortField+"\nsortOrder="+sortOrder);
    HashMap result = new Test.TestDB().SearchEmployees(key, pageIndex, pageSize, sortField, sortOrder);
    String json = Test.JSON.Encode(result);
    response.getWriter().write(json);
}

public void login(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    String json = request.getParameter("data");
    System.out.println("获得的页面登陆数据login:"+json);//这里打出你获得的页面数据
     
    ArrayList rows = (ArrayList)Test.JSON.Decode(json);
    HashMap row = (HashMap)rows.get(0);
    
    Userinformation userInformation = new Userinformation();
    userInformation.setUsername(ToString(row.get("username")));
    userInformation.setPwd(ToString(row.get("pwd")));
    
    System.out.println(userInformation.getUsername()+"\n pwd\n"+userInformation.getPwd());
    
     UserinformationDAO userinformationDAO = new UserinformationDAO();//操作userinformation表
     ArrayList list = (ArrayList)userinformationDAO.findByExample(userInformation);
     System.out.println("list.size"+list.size());
    if(list.size()!=0){
    	response.getWriter().write("success");
    	System.out.println("success");
    }else{
    	response.getWriter().write("fail");
    	System.out.println("fail");
    }
   	
}
public void register(HttpServletRequest request, HttpServletResponse response) throws Exception
{
    String json = request.getParameter("data");//这句话就是得到传过来的参数，就是页面输入的参数
    System.out.println("我们要在这里打出来注册的信息register:"+json);//然后我们打出来
    ArrayList rows = (ArrayList)Test.JSON.Decode(json);
   
    HashMap row = (HashMap)rows.get(0);
    
    Userinformation userInformation = new Userinformation();
    userInformation.setUsername(ToString(row.get("username")));
    userInformation.setPwd(ToString(row.get("pwd")));
    
    UserinformationDAO userinformationDAO = new UserinformationDAO();//操作userinformation表
    userinformationDAO.save(userInformation);
    
   	response.getWriter().write("注册成功");
}

public Applybase HashMapConvertApplyBase(HashMap row){
	Applybase applybase = new Applybase();
	//applybase.setApplyBaseId(ToInt(row.get("applyBaseId")));
	//System.out.println("ToInt applyBaseId"+ToInt(row.get("applyBaseId")));
	applybase.setName(ToString(row.get("name")));
	applybase.setGender(ToInt(row.get("gender")));
	applybase.setPhone(ToInt(row.get("phone")));
	applybase.setMail(ToString(row.get("mail")));
	applybase.setEducation(ToString(row.get("education")));
	applybase.setSchool(ToString(row.get("school")));
	applybase.setMajor(ToString(row.get("major")));
	applybase.setEnglish(ToString(row.get("English")));
	applybase.setComputer(ToString(row.get("computer")));
	applybase.setIdNum(ToInt(row.get("IdNum")));
	applybase.setSelfEvaluation(ToString(row.get("selfEvaluation")));
	return applybase;

}
private int ToInt(Object o){
	    	if(o == null) return 0;
	    	double d = Double.parseDouble(o.toString());
	    	int i = 0;
			i -= d;
			return -i;			
	    }    
	    private String ToString(Object o){
	    	if(o == null) return "";
	    	return o.toString();
	    }%> 